package com.xl.qa.controller;

import cn.hutool.core.util.StrUtil;
import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import com.xl.basic.annotation.LogIgnored;
import com.xl.basic.annotation.LoginIgnored;
import com.xl.basic.annotation.LoginValidated;
import com.xl.basic.entity.Result;
import com.xl.basic.entity.ResultEnums;
import com.xl.basic.entity.UserInfo;
import com.xl.basic.utils.LoginUtils;
import com.xl.qa.entity.param.AddQuesWxUnifiedOrderParam;
import com.xl.qa.entity.result.QuesWxUnifiedOrderResult;
import com.xl.qa.service.QuestionWxPayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 钟勇
 * Create in 2024/1/24 10:13
 */
@Tag(name = "问卷微信支付")
@RestController
@RequestMapping("/api/QuestionWxPay")
@RequiredArgsConstructor
@LoginIgnored
public class QuestionWxPayController {

    private final QuestionWxPayService payService;

    @Operation(summary = "创建问卷微信订单")
    @PostMapping(value = "/addQuesWxUnifiedOrder")
    @LoginValidated
    public Result<QuesWxUnifiedOrderResult> addQuesWxUnifiedOrder(@Validated AddQuesWxUnifiedOrderParam param) {
        Result<UserInfo> userInfoRes = LoginUtils.getUserInfo();
        UserInfo userInfo = userInfoRes.getResult();
        String openId = userInfo.getOpenId();
        if (StrUtil.isBlank(openId)) {
            return ResultEnums.BusinessError.getResult("请使用微信账号");
        }
        param.setOpenId(openId);

        return payService.addQuesWxUnifiedOrder(param);
    }

    @Operation(summary = "问卷订单微信支付统一回调")
    @PostMapping(value = "/wxQuesOrderPayNotify")
    @LogIgnored
    public String wxQuesOrderPayNotify(@RequestBody String xmlData) {
        Result<Object> result = payService.wxQuesOrderPayNotify(xmlData);
        if (result.isErr()) {
            return WxPayNotifyResponse.fail(result.getMessage());
        }
        return WxPayNotifyResponse.success("成功");
    }


}
